<template>
	<view class="cu-dialog">
		<view class="cu-bar bg-white justify-end solid-bottom">
			<view class="content">管理房源</view>
			<view class="action" @tap="hideModal">
				<text class="cuIcon-close text-red"></text>
			</view>
		</view>
		<view class="cu-form-group solid-bottom">
			<view class="title">房主:</view>
			<picker @change="PickerChange" :value="index" :range="houseOwnerRange" range-key="MemberName">
				<view class="picker">
					{{houseOwnerRange[index].MemberName}}
				</view>
			</picker>
		</view>
		<view class="cu-bar search bg-white solid-bottom">
			<view class="search-form ">
				<text class="cuIcon-search"></text>
				<input v-model.trim="search" :adjust-position="false" type="text" placeholder="请输入权证号" confirm-type="search"></input>
				<text class="input-clear cuIcon-close" v-if="search" @click="search=''"></text>
			</view>
			<view class="action" @click="handleSearch">
				<button class="cu-btn bg-blue shadow-blur">
					<text>搜索</text>
				</button>
			</view>
		</view>
		
		<view class="search-list">
			<radio-group @change="radioChange" class="block">			
				<view class="solid-bottom" v-for="(item,index) in searchList" :key="index" >
					<label class="cu-form-group" style="justify-content: flex-start;">
						<view class="title">
							  <radio class="round blue" :value="index" />								
						</view>
						<view>{{item.PlaceName }}</view>		
					</label>
				</view>			
			</radio-group>
		</view>
			
		<view class="cu-bar bg-white">
			<view class="action margin-0 flex-sub  solid-left" @tap="hideModal">取消</view>
			<view class="action margin-0 flex-sub  text-blue solid-left " @tap="submite(search)">确定</view>
		</view>
	</view>			
	
</template>

<!-- RoomId=签约室ID&Userid=房屋所有人&HouseId=房屋编号
 -->
<!-- 管理房源 -->
<script>
	import { getHouseInfo,  signRoomAddHouse } from '@/common/api/zft.js'
	export default {
		props: ['room'],
		data() {
			return {
				// 房主
				index: 0,
				searchIndex: '',
				// houseOwnerRange: ['张三', '李四', '王五', '赵六'],
				// 模态框中输入的值
				search: '',
				searchList: []
			}
		},
		computed: {
			houseOwnerRange() {
				return this.room.CS_RoomMember
			},
		},
		methods: {
			RegionChange(e) {
				// this.region = e.detail.value
				console.log(e)
			},
			addContract() {
				uni.navigateTo({
					url: '/pages/contract-edit/contract-edit',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			PickerChange(e) {
				this.index =  e.detail.value
			},
			radioChange(e) {
				console.log('e', e)
				this.searchIndex = Number(e.detail.value)
			},
			// 权证号搜索
			handleSearch() {
				this.searchList = []
				// identityNo=权利人证件号码&certificateNo=权证号
				if (this.search !== '') {
					let p = {
						identityNo: this.houseOwnerRange[this.index].MemberNo,
						certificateNo: this.search,
					}
					getHouseInfo(p, false).then(({data}) => {
						console.log('查找到的数据 ', data)
						if (data && data.msgcode == 1000) {
							if (data.data.length == 1) {
								this.searchList	= data.data;
							}
						} else this.showToast(data.msg)
					})
				} else this.showToast('请输入内容')
				
			},
			submite(val) {
				if(this.searchList.length <= 0 || this.searchIndex === '') {
					console.log(this.searchList, this.searchIndex)
					this.showToast('没有房屋数据, 请选择房屋')
					return
				}
				// RoomId=签约室ID&Userid=房屋所有人&HouseId=房屋编号
				let s = {
					RoomId: this.room.ID,
					Userid: this.houseOwnerRange[this.index].MemberUserid,
					HouseId: this.searchList[this.searchIndex].HouseId
				}				
				signRoomAddHouse(s).then(({data}) => {
					if (data && data.msgcode == 1000) {
						// this.$emit('changeRoom', data.data);
						this.$emit('changeHouse', [this.searchList[0]])
						this.hideModal();
						// this.$parent.getHouseInfo(this.searchList[0]);
						
					}else {
						this.showToast(data.msg)
					}
				})
				
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.$emit('hideModal')
			},
		}
	}
</script>
<style>
	.search-list {
		max-height: 50vh;
		overflow: auto;
	}
</style>
